package com.smsp.common;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.io.Serializable;

/**
 * @author DengWenJun
 * @cerateTime 2017-08-01-10:44
 **/
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//保证序列化json的时候，如果是null的对象，key也会消失
public class ServerResponse<T> implements Serializable {

    private int resultCode;
    private  String msg;
    private  T data;

    private  ServerResponse(int resultCode){
        this.resultCode=resultCode;
    }
    private ServerResponse(int resultCode,T data){
        this.resultCode=resultCode;
        this.data=data;
    }
    private ServerResponse(int resultCode,String msg,T data){
        this.resultCode=resultCode;
        this.msg=msg;
        this.data=data;
    }
    private ServerResponse(int resultCode,String msg){
        this.resultCode=resultCode;
        this.msg=msg;
    }
    @JsonIgnore
    //使之不在json序列化结果当中
    public boolean isSuccess(){
        return  this.resultCode==ResponseCode.SUCCESS.getCode();
    }
    public int getResultCode(){
        return resultCode;
    }
    public T getData(){
        return data;
    }
    public String getMsg(){
        return  msg;
    }
    public static <T> ServerResponse<T> createBySuccess(){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }
    public static <T> ServerResponse<T> cerateBySuccessMessage(String msg){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }
    public static <T>ServerResponse<T> cerateBySuccess(T data){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }
    public static <T>ServerResponse<T> cerateBySuccess(String msg,T data){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
    }

    public static <T> ServerResponse<T> cerateByError(){
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
    }

    public static <T> ServerResponse<T> createByErrorMessage(String errorMessage){
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMessage);
    }
    public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode,String errorMessage){
        return new ServerResponse<T>(errorCode,errorMessage);
    }
}

